package cn.tedu.yl.dataOverview.service.impl;
import cn.tedu.yl.dataOverview.mapper.dataOverviewMapper;
import cn.tedu.yl.dataOverview.pojo.vo.RechargeStatusList;
import cn.tedu.yl.dataOverview.pojo.vo.ReservationStatusList;
import cn.tedu.yl.dataOverview.pojo.vo.dataOverviewQueryVO;
import cn.tedu.yl.dataOverview.service.dataOverviewService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.*;
import java.util.stream.Stream;

@Service
@Slf4j
public class dataOverviewServiceImpl implements dataOverviewService {
    @Autowired
    private dataOverviewMapper dataOverviewMapper;

    //获取首页数据概况
    @Override
    public dataOverviewQueryVO getDataOverview() {
        log.info("进入首页获取数据概况业务方法内");
        dataOverviewQueryVO dataOverviewQuery = new dataOverviewQueryVO();

        //统计今日预约人数总数
        Long registerTotal = dataOverviewMapper.getRegisterTotal(new Date()) == null? 0 : dataOverviewMapper.getRegisterTotal(new Date());
        log.info("今日预约人数--" + registerTotal);

        //统计今日门诊消费金额总额
        Long spendTotal = dataOverviewMapper.getSpendTotal(new Date()) == null? 0 : dataOverviewMapper.getSpendTotal(new Date());
        log.info("消费金额总数--" + spendTotal);

        //统计今日门诊充值金额总额
        Long rechargeTotal = dataOverviewMapper.getRechargeTotal(new Date()) == null? 0 : dataOverviewMapper.getRechargeTotal(new Date());
        log.info("充值金额总数--" + rechargeTotal);

        //统计近7日预约情况
        List<LocalDate> dates = new ArrayList<>();
        Stream.iterate(LocalDate.now(), date -> date.minusDays(1))
                .limit(7)
                .forEach(date -> dates.add(date));
        //LocalDate 时间排序
        dates.sort(Comparator.naturalOrder());

        log.info("7天的日期:{}", dates);

        //统计近7日预约情况-----数据库中获取对应日期的预约人数和时间
        String start = dates.get(0).toString();
        String end = dates.get(dates.size() - 1).toString();
        List<ReservationStatusList> list = dataOverviewMapper.getReservationStatusList(start, end);

        log.info("预约人数:{}", list);

        //统计近7日预约情况-----放置7天的时间数据并且初始化
        List<ReservationStatusList> register = new ArrayList();

        for(int i = 0; i < dates.size(); i++) {
            ReservationStatusList statusList = new ReservationStatusList();
            statusList.setDate(dates.get(i).toString());
            statusList.setCount(0l);
            register.add(statusList);
        }

        if(list.size() > 0 && !list.isEmpty()){
            for(int i = 0; i < list.size(); i++) {
                for(int j = 0; j < register.size(); j++) {
                    if(list.get(i).getDate().split(" ")[0].equals(register.get(j).getDate())) {
                        register.get(j).setCount(list.get(i).getCount());
                    }
                }
            }
        }

        //统计近7日充值数据
        List<RechargeStatusList>  RechargelistSql = dataOverviewMapper.getRechargeStatusList(start, end);
        List<RechargeStatusList> recharge = new ArrayList();


        for(int i = 0; i < dates.size(); i++) {
            RechargeStatusList rechargeStatusList = new RechargeStatusList();
            rechargeStatusList.setDate(dates.get(i).toString());
            rechargeStatusList.setPaymentAmount("0");
            recharge.add(rechargeStatusList);
        }

        if(RechargelistSql.size() > 0 && !RechargelistSql.isEmpty()){
            for(int i = 0; i < RechargelistSql.size(); i++) {
                for(int j = 0; j < recharge.size(); j++) {
                    if(RechargelistSql.get(i).getDate().split(" ")[0].equals(recharge.get(j).getDate())) {
                        recharge.get(j).setPaymentAmount(RechargelistSql.get(i).getPaymentAmount());
                    }
                }
            }
        }

        log.info("近7日预约情况:{}",register);
        log.info("近7日充值数据:{}",recharge);

        //将数据返回
        dataOverviewQuery.setRegisterTotal(registerTotal);
        dataOverviewQuery.setSpendTotal(spendTotal);
        dataOverviewQuery.setRechargeTotal(rechargeTotal);
        dataOverviewQuery.setReservationData(register);
        dataOverviewQuery.setRechargeData(recharge);
        return dataOverviewQuery;
    }



}
